/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exemplo1;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JToggleButton;

/**
 *
 * @author Elise
 */
public class Exemplo1 extends JFrame {

    JLabel lampada;
    JToggleButton interruptor;

    Exemplo1() {
        interruptor = new JToggleButton(new ImageIcon(getClass().getResource("/src/imagens/b.jpg")));
        lampada = new JLabel(new ImageIcon(getClass().getResource("/src/imagens/apagada.jpg")));

        this.setLayout(new BorderLayout());
        this.add(lampada, BorderLayout.CENTER);
        this.add(interruptor, BorderLayout.EAST);
        this.setSize(700, 400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        /*Filho do Command implementa actionPerformed, que seria o Execute() do Command,
         * onde Command aqui ao invés de ser uma classe abstrata, é uma interface, implementada de forma inline*/
        ActionListener a = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (interruptor.isSelected()) {
                    lampada.setIcon(new ImageIcon(getClass().getResource("/imagens/acesa.jpg")));
                    //interruptor.setIcon(new ImageIcon(getClass().getResource("/imagens/p.jpg")));
                } else {
                    lampada.setIcon(new ImageIcon(getClass().getResource("/imagens/apagada.jpg")));
                    //interruptor.setIcon(new ImageIcon(getClass().getResource("/imagens/b.jpg")));
                }
            }
        };
        interruptor.addActionListener(a);

    }

    public static void main(String[] args) {
        Exemplo1 e = new Exemplo1();
        e.setVisible(true);
    }
}
